Skip to content

fix(neo4j): a junction without criteria adds nothing, and an empty block keeps its match in an or - #17011

Open
borinquenkid wants to merge 6 commits into
fix/neo4j-isnull-association-8.0.xfrom
fix/neo4j-empty-junction-8.0.x
Open

borinquenkid wants to merge 6 commits into
fix/neo4j-isnull-association-8.0.xfrom
fix/neo4j-empty-junction-8.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

Stacked on #16836 (base is its branch; merge that one first).

  • Neo4j: an empty and { } or or { } left a dangling AND or OR in the Cypher, which Neo4j rejected. Blank expressions are now dropped from a junction. A block without criteria inside an or no longer becomes an existence branch: it keeps the match, so it still asks for an associated node, as on the datastores that join.
  • Spec: EmptyJunctionSpec runs against a real Neo4j container. With the fix reverted, 3 of its 4 cases fail; the fourth (an empty association block in an or) pins behavior that was already right.

Full Neo4j core suite on this branch: 934 tests, 0 failures, 0 errors; codeStyle clean.

Fixes #14635 once Hibernate 7 and the in-memory/Hibernate 5 parts (#17010, #17009) are in; refs #14573 and #14617.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…ock keeps its match in an or

An empty and { } or or { } left a dangling AND or OR in the Cypher, which Neo4j
rejected. Blank expressions are dropped from a junction. A block without
criteria inside an or no longer becomes an existence branch: it keeps the match,
so it still asks for an associated entity, as on the datastores that join.

Fixes #14635

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
Copilot AI balanced review requested due to automatic review settings November 15, 2026 15:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An empty junction inside an association block still changes the match. A negated empty association block now fails to parse.

return dispatchCriterion(criterion.criteria, scope.forEnd(association))
}
if (scope.builder.inDisjunction) {
if (scope.builder.inDisjunction && !criterion.criteria.getCriteria().isEmpty()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other has no parent. or { parent { and {} }; eq('name', 'Other') } should match nothing, the same as parent {}. It returns Root and Other, because the empty and is treated as a branch. not { parent {} } used to find folders without a parent and now fails to parse. An empty junction adds nothing. A negated empty association still asks for the neighbour.

A junction without criteria produced no Cypher, so the callers dropped it: or {} found no restriction, not { and {} } found every node, and an empty not {} was accepted. Hibernate 5, the reference, treats an empty junction as true: it is neutral in an and, makes the or it stands in true and makes a negation false; an empty not {} is an error. The Cypher builder now answers a junction with no criteria with true and rejects an empty negation. A block without criteria still keeps the match and adds no condition, so it is no branch of the or it stands in.

Adds the shared table CriteriaEmptyJunctionSpec (52 shapes, Hibernate 5's results as the expected values); Neo4j skips a negated association block (it asks whether a neighbour exists) and the two detached inList rows (its inList reads only the listed values and ignores a subquery).

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

An empty junction is now true wherever it stands in Neo4j, as it is on Hibernate 5, the reference: it adds no restriction beside a property, makes the or it stands in true, is neutral in an and, and makes a negation false. An empty not {} throws, as on Hibernate 5. A junction without criteria used to produce no Cypher and the callers dropped it, so or {} restricted nothing and not { and {} } found every node. A block without criteria still keeps the match and adds no condition, so it is no branch of the or it stands in.

CriteriaEmptyJunctionSpec is the shared table: 52 shapes, with Hibernate 5's results as the expected values. 22 of its rows fail on Neo4j without the change; with it 49 pass and 3 are skipped, each for a stated reason in the spec: a negated association block (Neo4j asks whether a neighbour exists, so the negation keeps the nodes without one, where Hibernate joins), and the two detached inList rows (the Neo4j inList reads only its listed values and ignores a subquery, which is older than this change). Neo4j module: 986 tests, 0 failures; codeStyle clean.

The same table file is added by #17010 (8.0.x) and #17009 (7.0.x); the copies differ only in each branch's skip lists, so the merge-up keeps one copy and removes each store's skips as its fix lands.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An empty junction inside an or keeps its match. A negated empty association still fails to parse, and a negated association that holds an empty junction returns the wrong rows.

return dispatchCriterion(criterion.criteria, scope.forEnd(association))
}
if (scope.builder.inDisjunction) {
if (scope.builder.inDisjunction && !criterion.criteria.getCriteria().isEmpty()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not { parent {} } used to find folders without a parent. It now fails to parse, because the association adds a match and then an empty expression, and the negation wraps that as NOT (). not { child { or {} } } returns the orphan instead of nothing. An empty junction is true. A negated empty association still asks for the neighbour, and the query has to parse.

…ls it, and a negation with nothing to negate is refused

not { parent {} } failed to parse: the block adds a match and an empty expression, which the negation wrapped as NOT (). A
negation with nothing left to negate now throws the same IllegalArgumentException an empty not {} does, as Hibernate 5 does.

not { child { or {} } } returned the node without a child, because the block was an existence check under a NOT. A block
with criteria is a join on Hibernate 5, so its negation is a neighbour that fails the criteria and a node without a
neighbour is not in it. The negation is now the conjunction of the negations of its criteria, with such a block as an
existence check of a neighbour that fails its criteria.

CriteriaEmptyJunctionSpec gains three rows (not of a block with an and, with nothing, with a property) and Neo4j no longer
skips not { child { or {} } }. H5, H7 with the fix of #17010 and Neo4j agree on all of them.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Thank you. Two of the three are bugs and are fixed; the first I could not confirm as a bug, and I would like to check my reading with you.

not { parent {} } failing to parse: fixed. The block adds a match and an empty expression, and the negation wrapped that as NOT (). A negation with nothing left to negate now throws IllegalArgumentException("Logical expression [not] must contain at least 1 expression"), the same error an empty not {} raises, and the one Hibernate 5 raises for it.

not { child { or {} } } returning the orphan: fixed. A block with criteria is a join on Hibernate 5, so its negation is a neighbour that fails the criteria, and a node without a neighbour is not in it. The negation is now the conjunction of the negations of its criteria, with such a block as an existence check of a neighbour that fails its criteria. Neo4j no longer skips that row of CriteriaEmptyJunctionSpec.

or { parent { and {} }; eq name=Other }: I read Hibernate 5 differently. On a self-referencing entity (Root, a node with Root as its parent, and a node with none) Hibernate 5 returns the node that has a parent plus the node named Other, which is what Neo4j returns. The bare parent {} in an or returns nothing, because a block without criteria is no branch of the or and keeps its join. A block that holds and {} is a branch, and an empty junction is true. If you see Hibernate 5 return nothing for the and {} form, could you tell me the entity and the data? I will add it as a row. Until then the two forms stay as they are, and the table pins both.

Three rows are new (a negated block with an and, with nothing, and with a property). Hibernate 5, Hibernate 7 with the fix of #17010 and Neo4j agree on all three. Verified on the new head: the table fails without the fix (3 of 55) and passes with it, the Neo4j module is green (989 tests), and codeStyle is clean.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A negated empty association is refused instead of failing to parse, and a negated association that holds an empty junction returns nothing. A junction that holds only an empty association still drops the other branch.

try {
def inner = ((Query.Junction) criterion).criteria
.collect { Query.Criterion it -> dispatchCriterion(it, scope).toString() }
.findAll { String expression -> expression }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or { or { child {} }; eq('name', 'orphan') } should return the orphan, because the inner junction is true. It returns nothing. The empty association is filtered out, and the junction then adds nothing, so the required child match removes the name branch. An empty junction is true. Keep that when its children add no expression.

… junction without children is

or { or { child {} }; eq('name', 'orphan') } returned nothing: the inner junction holds only an empty
association block, which adds no expression, so the junction added nothing and was dropped from the
outer or. A junction whose children add no expression is true wherever it stands, as an empty one is.
Hibernate 5, the reference, returns the nodes that have a neighbour for this shape, and so does
Neo4j now. A negation of such a junction still has nothing to negate and is refused, as on
Hibernate 5.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley One of the three is fixed in the new commit. The other two already behave as you describe, and I checked each against Hibernate 5 first, as the reference.

Fixed: or { or { child {} }; eq('name', 'orphan') }. The inner junction holds only an empty association block, which adds no expression, so the junction added nothing and was dropped from the outer or; the query returned nothing. A junction whose children add no expression is now true wherever it stands, as one without children is. The result is not the orphan, though: Hibernate 5 returns [other, with] for this shape, because the empty child {} still asks for a neighbour, so the node without one is left out. Neo4j returns the same now. I added it to the shared table (CriteriaEmptyJunctionSpec) with the Hibernate 5 result; it passes on Hibernate 5, on Hibernate 7 with the #17010 change, and on Neo4j, and it fails on Neo4j without this commit.

If you expect the orphan from this shape, that is a difference from Hibernate 5 and not from Neo4j, and it would need a separate change for all three stores. Say so and I will open it.

Already as you describe:

  • not { child { or {} } } returns nothing (table row not { child { or {} } }, expected []).
  • not { child {} } is refused with Logical expression [not] must contain at least 1 expression, as on Hibernate 5, where the same row expects an exception. It is no longer a parse error. I kept the refusal because the table pins it for Hibernate 5; if you want it to return nothing instead, that is again a change for every store.

A negation of a junction that adds no expression still has nothing to negate and is refused, so not { child {} } keeps its behaviour with this change. The Neo4j module (1,093 tests) and codeStyle pass.

Branch policy: the other new throws in this PR were already in it. This commit adds none. Where the PR belongs is still open; I have not moved it.

If you find another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants